<template>
  <div class="detail">
    <el-card>
      <el-tabs>
        <el-tab-pane class="box" label="登录账号设置" style="width: 400px">
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="姓名" prop="username">
              <el-input v-model="form.username" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="form.password" type="password" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submit">更新</el-button>
              <el-button>返回</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情">
          <component
            :is="'Info'"
            :user-info="otherUserInfo"
            @updateUserInfo="updateUserInfo"
            @getData="getData"
          />
        </el-tab-pane>
        <el-tab-pane label="岗位详情">
          <component :is="'Job'" />
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>
<script>
import { sysUser } from '@/api/user'
import { sysUserPut } from '@/api/employees.js'
export default {
  name: '',
  components: {
    Info: () => import('./component/info.vue'),
    Job: () => import('./component/job.vue')
  },
  data() {
    return {
      id: this.$route.params.id,
      otherUserInfo: {},
      // 防止密码受到邮箱 单独定义userInfo对象
      userInfo: '',
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'change' }
        ],
        password: [{ required: true, message: '请输入姓名', trigger: 'change' }]
      }
    }
  },
  computed: {},
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      const res = await sysUser(this.id)
      console.log(res, 888888)
      this.form.username = res.data.username
      // 父组件用户信息与子组件不能形成关联
      this.otherUserInfo = JSON.parse(JSON.stringify(res.data))
      // 防止密码受到影响 单独定义userInfo对象
      this.userInfo = res.data
    },
    submit() {
      this.$refs.form.validate(async(valid) => {
        if (valid) {
          await sysUserPut({ ...this.userInfo, ...this.form })
          // 父组件的值修改了，子组件也需要更新
          this.otherUserInfo.username = this.form.username
          this.$message.success('修改成功')
          this.$store.dispatch('user/getUserInfo')
          // this.$store.commit('user/logout')
          // this.$router.push('/login')
        }
      })
    },
    // 子组件修改了，父组件也要修改
    updateUserInfo(info) {
      this.form.username = info.username
      this.userInfo = JSON.parse(JSON.stringify(info))
    }
  }
}
</script>
<style lang="scss" scoped>
.detail {
  padding: 15px;
}
.box {
  margin: 20px auto;
}
</style>
